package com.ruoyi.file.service.impl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.file.service.IImageAuditService;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.ims.v20201229.ImsClient;
import com.tencentcloudapi.ims.v20201229.models.ImageModerationRequest;
import com.tencentcloudapi.ims.v20201229.models.ImageModerationResponse;

/**
 * 图片内容审核服务实现类
 *
 * @author 区嘉盛
 */
@Service
public class ImageAuditServiceImpl implements IImageAuditService {

    @Value("${spring.tencent.cloud.secretId}")
    private String secretId;

    @Value("${spring.tencent.cloud.secretKey}")
    private String secretKey;

    @Value("${spring.tencent.cloud.region}")
    private String region;

    @Value("${spring.tencent.cloud.tms.bizType}")
    private String bizType;

    /**
     * 审核图片内容
     *
     * @param fileContent 图片Base64编码内容
     * @return 审核结果 PASS或REJECT
     */
    @Override
    public String auditImageContent(String fileContent) {
        try {
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ims.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            ImsClient client = new ImsClient(cred, region, clientProfile);

            ImageModerationRequest req = new ImageModerationRequest();
            req.setFileContent(fileContent);
            req.setBizType(bizType);

            ImageModerationResponse resp = client.ImageModeration(req);
            String suggestion = resp.getSuggestion();

            if ("Block".equals(suggestion) || "Review".equals(suggestion)) {
                return "REJECT";
            } else {
                return "PASS";
            }
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            return "PASS"; // 出错时默认通过，避免影响正常业务
        } catch (Exception e) {
            e.printStackTrace();
            return "PASS";
        }
    }

    /**
     * 审核图片内容
     *
     * @param fileUrl 图片URL
     * @return 审核结果 PASS或REJECT
     */
    @Override
    public String auditImageContentByUrl(String fileUrl) {
        try {
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ims.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            ImsClient client = new ImsClient(cred, region, clientProfile);

            ImageModerationRequest req = new ImageModerationRequest();
            req.setFileUrl(fileUrl);
            req.setBizType(bizType);

            ImageModerationResponse resp = client.ImageModeration(req);
            String suggestion = resp.getSuggestion();

            if ("Block".equals(suggestion) || "Review".equals(suggestion)) {
                return "REJECT";
            } else {
                return "PASS";
            }
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            return "PASS"; // 出错时默认通过，避免影响正常业务
        } catch (Exception e) {
            e.printStackTrace();
            return "PASS";
        }
    }
}